﻿using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for SMTP
/// </summary>
public class SMTP
{
    public bool SendEmail(string fromMail, string toMail, string subject, string mailBody, bool isBodyHtml, string mailServer, string loginName, string loginPassword)
    {
        try
        {
            // setup email header
            System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

            // Set the message sender
            // sets the from address for this e-mail message.
            mailMessage.From = new System.Net.Mail.MailAddress("support@parkerpen.com.vn");
            // Sets the address collection that contains the recipients of this e-mail message.
            mailMessage.To.Add(new System.Net.Mail.MailAddress(toMail));

            System.Net.Mail.MailAddress copy = new System.Net.Mail.MailAddress("kiennnit@yahoo.com");
            //mailMessage.CC.Add(copy);
            mailMessage.Bcc.Add(copy);

            // sets the message subject.
            mailMessage.Subject = subject;
            // sets the message body.
            mailMessage.Body = mailBody;
            // sets a value indicating whether the mail message body is in Html.
            // if this is false then ContentType of the Body content is "text/plain".
            mailMessage.IsBodyHtml = isBodyHtml;

            // add all the file attachments if we have any
            //if (_Attachments != null && _Attachments.Length > 0)
            //    foreach (string _Attachment in _Attachments)
            //        mailMessage.Attachments.Add(new System.Net.Mail.Attachment(_Attachment));

            // SmtpClient Class Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).
            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(mailServer);

            //Specifies how email messages are delivered. Here Email is sent through the network to an SMTP server.
            //smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            // Some SMTP server will require that you first authenticate against the server.
            // Provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication.
            System.Net.NetworkCredential _NetworkCredential = new System.Net.NetworkCredential(loginName, loginPassword);
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = _NetworkCredential;
            //Let's send it
            smtpClient.EnableSsl = false;
            smtpClient.Send(mailMessage);

            // Do cleanup
            mailMessage.Dispose();
            smtpClient = null;
        }
        catch (Exception _Exception)
        {
            // Error
            //Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
            return false;
        }

        return true;
    }
}
